package org.jeecg.modules.wlhy.hycustomerbank.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.wlhy.hycustomerbank.entity.HyCustomerBank;
import org.jeecg.modules.wlhy.hycustomerbank.mapper.HyCustomerBankMapper;
import org.jeecg.modules.wlhy.hycustomerbank.service.IHyCustomerBankService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description: 货主银行卡信息
 * @Author: duanlikao
 * @Date:   2020-12-15
 * @Version: V1.0
 */
@Service
public class HyCustomerBankServiceImpl extends ServiceImpl<HyCustomerBankMapper, HyCustomerBank> implements IHyCustomerBankService {

    private static final Logger LOGGER = LoggerFactory.getLogger(HyCustomerBankServiceImpl.class);

    @Override
    public Result <?> saveCustomerBank(HyCustomerBank hyCustomerBank) {


        Result<JSONObject> result = new Result<JSONObject>();
        HyCustomerBank hyCustomerBankDb = null;
        if(StringUtils.isNotEmpty(hyCustomerBank.getId())   ){
            hyCustomerBankDb = getById(hyCustomerBank.getId());
            if(hyCustomerBankDb==null) {
                throw new JeecgBootException("未找到对应数据");
            }
        }
        boolean resultSave ;
        if(StringUtils.isNotEmpty(hyCustomerBank.getId())  ){
            resultSave = updateById(hyCustomerBank);
        }else{
            resultSave =  save(hyCustomerBank);
        }
        result.setSuccess(false);
        result.setCode(CommonConstant.REGISTER_FAILED);
        result.setMessage("货主银行卡信息保存失败,请重新提交信息！");
        result.setTimestamp((new Date()).getTime());
        if(resultSave){
            result.setSuccess(true);
            result.setCode(CommonConstant.SC_OK_200);
            result.setMessage("货主银行卡信息保存成功!");
            result.setResult( hyCustomerBankDb == null ?
                    JSONObject.parseObject(JSONObject.toJSONString(hyCustomerBank) ):
                    JSONObject.parseObject(JSONObject.toJSONString(hyCustomerBankDb) )
            );
        }

        return result;
    }


    /**
     * 货主银行卡解绑
     * @param id
     * @return
     */
    @Override
    public Result <?> unBundLing(String id){

        Result<JSONObject> result = new Result<>();
        HyCustomerBank hyCustomerBankDb = null;
        if(StringUtils.isNotEmpty(id)){
            hyCustomerBankDb = getById(id);
            if(hyCustomerBankDb==null) {
                throw new JeecgBootException("未找到对应数据");
            }
        }
        hyCustomerBankDb.setIzUsed("0");
        result.setSuccess(false);
        result.setCode(CommonConstant.REGISTER_FAILED);
        result.setMessage("货主银行卡解绑失败,请重新提交信息！");
        result.setTimestamp((new Date()).getTime());
        if(updateById(hyCustomerBankDb)){
            result.setSuccess(true);
            result.setCode(CommonConstant.SC_OK_200);
            result.setMessage("货主银行卡解绑成功!");
        }

        return result;
    }
}
